package com.grandtech.insurance.common.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * 执行linux命令工具类
 */
public class ExcuteLinux {

    public static void main(String[] args) {
//        String result = ExcuteLinux.exeCmd("ifconfig");
//        System.out.println("获取的结果是"+"\n"+result);
        String yxPath = "/home/cid-sharedir/dt_meta_cgyx/130181辛集市_作物长势解译成果202002.tif.123";
        String path1 = yxPath.substring(0,yxPath.lastIndexOf("/")+1);
        String fileName = yxPath.substring(yxPath.lastIndexOf("/")+1,yxPath.lastIndexOf("."));
        String hz = yxPath.substring(yxPath.indexOf("."));
        System.out.println(hz);
        System.out.println(path1);
        System.out.println(fileName);
    }

    public static String exeCmd(String commandStr) {
        String result = null;
        try {
            String[] cmd = new String[]{"/bin/sh", "-c",commandStr};
            Process ps = Runtime.getRuntime().exec(cmd);
            //Process ps = Runtime.getRuntime().exec(commandStr);

            BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = br.readLine()) != null) {
                //执行结果加上回车
                sb.append(line).append(";");
            }
            result = sb.toString();
        } catch (Exception e) {
            System.out.println("执行linux命令:"+commandStr+"失败....");
            e.printStackTrace();
        }

        return result;

    }
}
